var wxpay = require('../../utils/pay.js')
var req = require('../../utils/reqUtils.js')
var app = getApp()
Page({
  data: {
    statusType: ["全部", "已预约", "已完成", '已取消'],//"待发货","待收货",
    currentTpye: 0,
    tabClass: ["", "", "", "", ""]
  },
  statusTap: function (e) {
    var curType = e.currentTarget.dataset.index;
    this.data.currentTpye = curType
    this.setData({
      currentTpye: curType
    });
    this.onShow();
  },
  orderDetail: function (e) {
    var orderId = e.currentTarget.dataset.id;
    // wx.navigateTo({
    //   url: "/pages/order-details/index?id=" + orderId
    // })
  },
  cancelOrderTap: function (e) {
    var that = this;
    var orderId = e.currentTarget.dataset.id;
    wx.showModal({
      title: '确定要取消该订单吗？',
      content: '',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading();
          wx.request({
            url: 'https://api.it120.cc/' + app.globalData.subDomain + '/order/close',
            data: {
              token: app.globalData.token,
              orderId: orderId
            },
            success: (res) => {
              wx.hideLoading();
              if (res.data.code == 0) {
                that.onShow();
              }
            }
          })
        }
      }
    })
  },
  toPayTap: function (e) {
    var orderId = e.currentTarget.dataset.id;
    var that = this;
    req.post('/cancel_appointment_course/index', { 'id': orderId }, 1).then(res => {

      console.log(res.data);
      this.onShow();
    });
    // var money = e.currentTarget.dataset.money;
    // wxpay.wxpay(app, money, orderId, "/pages/order-list/index");
  },
  onLoad: function (options) {
    // 生命周期函数--监听页面加载

  },
  onReady: function () {
    // 生命周期函数--监听页面初次渲染完成

  },
  dateTime: function (date, time) {
    let year = new Date(date).getFullYear();
    let month = new Date(date).getMonth() + 1;
    let day = new Date(date).getDate();
    let minutes = new Date(date).getMinutes() + time % 60;
    let ifHous = 0;
    if (minutes >= 60) {
      ifHous = 1;
      minutes = '00';
    }
    let hours = new Date(date).getHours() + Math.floor(time / 60) + ifHous;
    return year + '-' + month + '-' + day + ' ' + (hours >= 10 ? hours : ('0' + hours)) + ":" + minutes + ':00';
  },
  onShow: function () {
    // 获取订单列表
    wx.showLoading();
    var that = this;
    console.log(that.data.currentTpye);
    req.post('/user_appointment_course/index', { 'id': wx.getStorageSync('userInfo')['u_id'], 'state': that.data.currentTpye }, 1).then(res => {
      (res.data.courseList).forEach(element => {
        element.end_date = this.dateTime(element.start_date, element.get_card.duration);
      });

      console.log(res.data.courseList);
      wx.hideLoading();
      that.setData({
        orderList: res.data.courseList,
        // logisticsMap : res.data.data.logisticsMap,
        // goodsMap : res.data.data.goodsMap
      });
    });

  },
  onHide: function () {
    // 生命周期函数--监听页面隐藏

  },
  onUnload: function () {
    // 生命周期函数--监听页面卸载
  },
  onPullDownRefresh: function () {
    // 页面相关事件处理函数--监听用户下拉动作

  },
  onReachBottom: function () {
    // 页面上拉触底事件的处理函数

  }
})